/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.service.plugin;

import com.apache.api.vo.ResultEntity;
import com.apache.portal.common.PortalPlugin;
import com.apache.portal.common.annotion.AnntationBean;
import com.apache.portal.common.util.PortalPubFactory;
import com.apache.rpc.common.LoadRpcService;
import com.apache.tools.DateUtils;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@AnntationBean(name = "audit")
public class AuditSystemPluginImpl implements PortalPlugin {

    private Logger log = Logger.getLogger(AuditSystemPluginImpl.class);

    public Object doInvoke(HttpServletRequest request, HttpServletResponse response,
            Map<String, String> params) {
        String doCode = params.get("doCode");
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setResult("F");
        resultEntity.setMessage("执行失败！");
        Map<String, Object> paramsMap = new HashMap();
        paramsMap.putAll(params);
        if ("auditSave".equals(doCode)) {
            return auditSave(request, paramsMap);
        }
        if ("auditList".equals(doCode)) {
            return auditList(request, paramsMap);
        }
        if ("auditInfo".equals(doCode)) {
            return auditInfo(request, paramsMap);
        }
        return resultEntity;
    }

    private ResultEntity auditSave(HttpServletRequest request, Map<String, Object> params) {
        ResultEntity resultEntity = new ResultEntity();
        params.put("createTime", DateUtils.Now.fmt_yyyyMMdd_HHmmss());
        ResultEntity entity = LoadRpcService.service()
                .doServiceClient("auditService", "auditSave", params,
                        PortalPubFactory.getInstance().getRpcInfo("audit"));
        if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
            resultEntity.setResult("T");
            String id = entity.getEntity().toString();
            resultEntity.setEntity(id);
        } else {
            resultEntity.setResult("F");
        }
        resultEntity.setMessage(entity.getMessage());

        return resultEntity;
    }

    private Object auditList(HttpServletRequest request, Map<String, Object> params) {
        ResultEntity entity = (ResultEntity) LoadRpcService.service()
                .doServiceClient("auditService", "auditList", params,
                        PortalPubFactory.getInstance().getRpcInfo("audit"));
        return entity;
    }

    private Object auditInfo(HttpServletRequest request, Map<String, Object> params) {
        ResultEntity entity = LoadRpcService.service()
                .doServiceClient("auditService", "auditInfo", params,
                        PortalPubFactory.getInstance().getRpcInfo("audit"));
        return entity;
    }
}
